﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace uban.demo.Protocol
{
    public class KeyStatue
    {
        private const int bufferLen = 4;
        /// <summary>
        /// 按键
        /// </summary>
        private char _key;
        /// <summary>
        /// 按键类型
        /// </summary>
        private KeyType _keyType;
        /// <summary>
        /// 按键
        /// </summary>
        public char Key
        {
            get
            {
                return _key;
            }
        }
        /// <summary>
        /// 按键类型
        /// </summary>
        public KeyType KeyInputType
        {
            get
            {
                return _keyType;
            }            
        }

        /// <summary>
        /// 按键类型
        /// </summary>
        public enum KeyType
        {
            /// <summary>
            /// 无效按键
            /// </summary>
            None,
            /// <summary>
            /// 正常按键
            /// </summary>
            Normal,
            /// <summary>
            /// 长按
            /// </summary>
            LongPress,
            /// <summary>
            /// 关机
            /// </summary>
            Power
        }
        /// <summary>
        /// 构造按键
        /// </summary>
        /// <param name="raw">按键字节流</param>
        public KeyStatue(byte[] raw)
        {
            Process(raw);
        }
        /// <summary>
        ///A： 0A E1 F5 05
        ///B： 0B E1 F5 05
        ///C： 0C E1 F5 05
        ///D： 0D E1 F5 05
        ///A⻓按：AA E1 F5 05
        ///B⻓按：BB E1 F5 05
        ///C⻓按：CC E1 F5 05
        ///D⻓按：DD E1 F5 05
        ///对号： 00 E1 F5 05
        ///×： 01 E1 F5 05
        ///对号⻓按： 02 E1 F5 05
        ///×⻓按： 03 E1 F5 05
        ///开关键： 04 E1 F5 05
        /// </summary>
        private void Process(byte[] raw)
        {
            if(raw==null || raw.Length != bufferLen)
            {
                _keyType = KeyType.None;
            }else if(raw[1] == 0xE1 && raw[2] == 0xF5 && raw[3] == 0x05)
            {
                switch (raw[0])
                {
                    case 0x0A:
                        _key = 'A';
                        _keyType = KeyType.Normal;
                        break;
                    case 0x0B:
                        _key = 'B';
                        _keyType = KeyType.Normal;
                        break;
                    case 0x0C:
                        _key = 'C';
                        _keyType = KeyType.Normal;
                        break;
                    case 0x0D:
                        _key = 'D';
                        _keyType = KeyType.Normal;
                        break;
                    case 0xAA:
                        _key = 'A';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0xBB:
                        _key = 'B';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0xCC:
                        _key = 'C';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0xDD:
                        _key = 'D';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0x00:
                        _key = '√';
                        _keyType = KeyType.Normal;
                        break;
                    case 0x01:
                        _key = '×';
                        _keyType = KeyType.Normal;
                        break;
                    case 0x02:
                        _key = '√';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0x03:
                        _key = '×';
                        _keyType = KeyType.LongPress;
                        break;
                    case 0x04:
                        _key = '\0';
                        _keyType = KeyType.Power;
                        break;
                }
            }
            else
            {
                _keyType = KeyType.None;
            }
        }
        public override string ToString()
        {
            string keyDescrption = "";
            switch (_keyType)
            {
                case KeyType.None:
                    keyDescrption = "无效按键";
                    break;
                case KeyType.Normal:
                    keyDescrption = $"{_key}";
                    break;
                case KeyType.LongPress:
                    keyDescrption = $"长按{_key}";
                    break;
                case KeyType.Power:
                    keyDescrption = "关机";
                    break;
            }
            return $"你的按键：{keyDescrption}";
        }
    }
}
